//
//  FinallyCaculateViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/4/21.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class FinallyCaculateViewController: BaseViewController {

    var price = ""
    var number = 10000
    var param : Dictionary<String,String> = [:]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.naviHide = false
        self.defaultBackButton()
        self.view.backgroundColor = UIColor.colorWithHexString(hex: "f9f9f9")
        configViews()
    }
    
    func configViews() -> Void {
        
        let priceBlock = PriceBlockView.init(frame: CGRect.init(x: 30 * YXWidthScale, y: 20 * YXHeightScale, width: YXDeviceWidth - 60 * YXWidthScale, height: 402 * YXHeightScale), price: price)
        self.view.addSubview(priceBlock)
        
        let reminderLbl = UILabel.init()
        
        let i = StringToInt.toInt(source: price)
        
        let random = Int((0.05 + CGFloat(arc4random() % 5) * 0.01) * CGFloat(i))
        
        reminderLbl.text = "预计下周价格下降" + String(random) + "元"
        
        reminderLbl.textColor = UIColor.colorWithHexString(hex: "2c2c2c")
        
        reminderLbl.font = UIFont.boldSystemFont(ofSize: 30 * YXWidthScale)
        
        let sizes = reminderLbl.text!.StringSize(font: reminderLbl.font)
        
        reminderLbl.frame = CGRect.init(x: YXDeviceWidth / 2 - sizes.width / 2, y: priceBlock.bottom() + 60 * YXHeightScale, width: sizes.width, height: sizes.height)
        
        self.view.addSubview(reminderLbl)
        
        let priceTent = PriceTendView.init(frame: CGRect.init(x: 0, y: reminderLbl.bottom() + 10 * YXHeightScale, width: YXDeviceWidth, height: 275 * YXHeightScale))
        
        let currentPrice = StringToInt.toInt(source: price)
        
        var delt = currentPrice / 10
        
        if delt == 0 {
            delt = 1
        }
        
        let rand1 = (CGFloat(arc4random() % 5) * 0.1 + 0.5) * CGFloat(delt)
//        print(rand1)
        let forePrice = currentPrice + Int(rand1)
        
        let rand2 = (CGFloat(arc4random() % 5) * 0.1 + 0.1) * CGFloat(delt)
//        print(rand2)
        let foreforePrice = forePrice + Int(rand2)
        
        priceTent.prices = [foreforePrice,forePrice,currentPrice]
        priceTent.months = Date.recentThreeMonths()
        self.view.addSubview(priceTent)
        
        let trayView = UIView.init(frame: CGRect.init(x: 120 * YXWidthScale, y: priceTent.bottom() + 60 * YXHeightScale, width: YXDeviceWidth - 249 * YXHeightScale, height: 54 * YXHeightScale))
        let label1 = UILabel.init()
        label1.text = "您是第"
        label1.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        let size = label1.text!.StringSize(font: label1.font)
        label1.frame = CGRect.init(x: 0, y: 0, width: size.width, height: trayView.height())
        label1.textColor = UIColor.colorWithHexString(hex: "666666")
        label1.textAlignment = .center
        trayView.addSubview(label1)
        let collection = NumberPadCollectionView.init(frame: CGRect.init(x: label1.right() + 20 * YXWidthScale, y: 0, width: 346 * YXWidthScale, height: trayView.frame.size.height))
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.init(uptimeNanoseconds: 2), execute: {()in
            if self.number > 999999999 {
                self.number = 999999999
            }
            
            if self.number < 0 {
                self.number = 0
            }
            collection.scroll(number: self.number)
        })
        trayView.addSubview(collection)
        let label2 = UILabel.init()
        label2.textColor = UIColor.colorWithHexString(hex: "666666")
        label2.text = "位"
        label2.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        let size1 = label2.text!.StringSize(font: label2.font)
        label2.frame = CGRect.init(x: collection.right() + 20 * YXWidthScale, y: 0, width: size1.width, height: trayView.height())
        trayView.addSubview(label2)
        self.view.addSubview(trayView)
        
        let label3 = UILabel.init()
        label3.textColor = UIColor.colorWithHexString(hex: "666666")
        label3.text = "用大象回收进行检测的用户"
        label3.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        let size2 = label3.text!.StringSize(font: label3.font)
        label3.frame = CGRect.init(x: trayView.left(), y: trayView.bottom() + 40 * YXHeightScale, width: size2.width, height: size2.height)
        self.view.addSubview(label3)
        
        let sure = UIButton.init(type: .system)
        sure.backgroundColor = UIColor.colorWithHexString(hex: "2c2c2c")
        sure.setTitleColor(UIColor.white, for: .normal)
//        sure.setTitleColor(UIColor.black, for: .disabled)
        sure.setTitle("确定", for: .normal)
        sure.titleLabel?.font = UIFont.boldSystemFont(ofSize: 36 * YXWidthScale)
        
        sure.frame = CGRect.init(x: (YXDeviceWidth - 360 * YXWidthScale) / 2 , y: label3.bottom() + 60 * YXHeightScale, width: 360 * YXWidthScale, height: 88 * YXHeightScale)
        sure.addTarget(self, action: #selector(settleAccounts(btn:)), for: .touchUpInside)
        sure.layer.cornerRadius = YXButtonCorner
        self.view.addSubview(sure)
    }
    
    @objc func settleAccounts(btn : UIButton) -> Void {
        self.alertSomeMessage(title: YXAlertTitle, content: "是否结算?", sureTitle: YXConfirmTitle, cancelTitle: YXCancelTitle, ensureAction: {(alert) in
            self.anotherSettle(btn: btn)
        }, cancelAction: {(alert) in
            
        })
    }
    
    func anotherSettle(btn : UIButton) -> Void {
        btn.isEnabled = false
        self.beginAnim()
        for key in param.keys {
            if param[key] == nil {
                return
            }
        }
        Manager.shared().addCompletedOrder(param: param, success: {(mod) in
            self.endAnim()
            if mod.result == true {
                SimpleAlertView.show(dismissBlock: {
                    btn.isEnabled = true
                    let information = GetInformationViewController.init()
                    information.param = self.param
                    self.navigationController?.pushViewController(information, animated: true)
                },superView: self.view)
            } else {
                btn.isEnabled = true
                self.commonAlertMsg(content: mod.msg)
            }
        }, fail: {(code) in
            self.endAnim()
            btn.isEnabled = true
            self.commonAlertMsg(content: YXNetWorkFail)
        })
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
